/**
 * 表单视图组件
 */
import React, { Component } from 'react';

import UIForm from './UIForm';
import UIView from './UIView';
import { isNotNull } from '../common';

export default class UIFormView extends Component{
	
    // constructor(props) {
    //     super(props);
    // }
    
	componentDidMount() {
		const { innerView } = this.refs;
		const { toolbar } = innerView.refs;
		const { setToolbarState} = this.props;
		
		if(setToolbarState !== undefined && toolbar){
			setToolbarState(toolbar,innerView);
		}
	}

	setDisabledToolbarItems = (toolbarItems) => {
		const { innerView } = this.refs;
		const { toolbar } = innerView.refs;

		toolbarItems.forEach( (item) => {
			toolbar.disable(item);
		} );
	}

	setEnabledToolbarItems = (toolbarItems) => {
		const { innerView } = this.refs;
		const { toolbar } = innerView.refs;

		toolbarItems.forEach( (item) => {
			toolbar.enable(item);
		} );
	}

	setDisabledValues = (fieldId,values) => {
		this.form.setDisabledValues(fieldId,values);
	}
	
	isOpend = () =>{
	 	const { innerView } = this.refs;
		return innerView.isOpend();
	 }
	
	 closeModal = (e) =>{
	 	const { innerView } = this.refs;
		innerView.closeModal();
	 }
	
	openModal = (cb) =>{
		const { innerView } = this.refs;
		innerView.openModal(cb);
	}
	
	getFormData = (cb) => {
		this.form.getFormData(cb);
	}
  		
  	setFormData = (formData,callback) => {
		if(isNotNull(this.form)){
			this.form.setFormData(formData,callback);
		}
		
	}
	  
	setFieldsValue = (fieldValue) => {
		this.form.setFieldsValue(fieldValue);
	}
  	
  	resetForm = () => {
  		this.form.resetForm();
	  }
	  
	setFormParams = (params) => {
		if(isNotNull(this.form)){
			this.form.setFormParams(params);
		}
	}
	
	setFormFieldParams = (fieldId,params) => {
		if(isNotNull(this.form)){
			this.form.setFormFieldParams(fieldId,params);
		}
	}
	
	render() {
		
		return (
			<UIView {...this.props} parent={this} ref="innerView">
				<UIForm
					wrappedComponentRef={(inst) => this.form = inst}
					parent={this}
					formConfigId={this.props.formConfigId}
					history={this.props.history}
					location={this.props.location}
					params={this.props.params}
					disabledFileds={this.props.disabledFileds}
					disabledValues={this.props.disabledValues}
					onValuesChange={this.props.onValuesChange}
					fieldRender={this.props.fieldRender}
					fieldPropsMap={this.props.fieldPropsMap}
				>
				</UIForm>
				{this.props.children}
			</UIView>
		);
	}
}
